/*
 ============================================================================
 文件名 	: EasyBounceAppUi.cpp
 描述		: CEasyBounceAppUi类的实现.
 ============================================================================
 */

//引用
#include <avkon.hrh>
#include <aknmessagequerydialog.h>
#include <aknnotewrappers.h>
#include <stringloader.h>
#include <f32file.h>
#include <s32file.h>

#include <EasyBounce.rsg>

#include "EasyBounce.hrh"
#include "EasyBounce.pan"
#include "EasyBounceApplication.h"
#include "EasyBounceAppUi.h"
#include "EasyBounceMainView.h"
#include "EasyBounceGameView.h"
#include "EasyBounceRankView.h"

// ============================ 成员函数 ===============================


// -----------------------------------------------------------------------------
// CEasyBounceAppUi::ConstructL()
// 两阶段构造函数
// -----------------------------------------------------------------------------
//
void CEasyBounceAppUi::ConstructL()
	{
	// 初始化appui
	BaseConstructL(CAknAppUi::EAknEnableSkin);
	
	CEasyBounceMainView* mainView = new (ELeave) CEasyBounceMainView;
    CleanupStack::PushL( mainView );
    mainView->ConstructL();
    AddViewL( mainView );      // 将所有权交给CAknViewAppUi
    CleanupStack::Pop( mainView ); 
    
    CEasyBounceGameView* gameView = new (ELeave) CEasyBounceGameView;
    CleanupStack::PushL( gameView );
    gameView->ConstructL();
    AddViewL( gameView );      // 将所有权交给CAknViewAppUi
    CleanupStack::Pop( gameView ); 
    
    CEasyBounceRankView* rankView = new (ELeave) CEasyBounceRankView;
    CleanupStack::PushL( rankView );
    rankView->ConstructL();
    AddViewL( rankView );      // 将所有权交给CAknViewAppUi
    CleanupStack::Pop( rankView ); 
    
    SetDefaultViewL( *mainView );
	
	}
// -----------------------------------------------------------------------------
// CEasyBounceAppUi::CEasyBounceAppUi()
// C++默认构造函数，为防止Leave，此处不应包含任何代码.
// -----------------------------------------------------------------------------
//
CEasyBounceAppUi::CEasyBounceAppUi()
	{
	//空实现
	}

// -----------------------------------------------------------------------------
// CEasyBounceAppUi::~CEasyBounceAppUi()
// 析构函数.
// -----------------------------------------------------------------------------
//
CEasyBounceAppUi::~CEasyBounceAppUi()
	{
	//空实现
	}

// -----------------------------------------------------------------------------
// CEasyBounceAppUi::HandleCommandL()
// 处理菜单命令
// -----------------------------------------------------------------------------
//
void CEasyBounceAppUi::HandleCommandL(TInt aCommand)
	{
	switch (aCommand)
		{
		case EEikCmdExit:
		case EAknSoftkeyExit:
			Exit();
			break;

		//弹出“关于”对话框
		case ECmdAbout:
			{
			CAknMessageQueryDialog* dlg = new (ELeave) CAknMessageQueryDialog();
			dlg->PrepareLC(R_ABOUT_QUERY_DIALOG);
			
			HBufC* title = iEikonEnv->AllocReadResourceLC(R_ABOUT_DIALOG_TITLE);
			dlg->QueryHeading()->SetTextL(*title);
			CleanupStack::PopAndDestroy(); //title
			
			HBufC* msg = iEikonEnv->AllocReadResourceLC(R_ABOUT_DIALOG_TEXT);
			dlg->SetMessageTextL(*msg);
			CleanupStack::PopAndDestroy(); //msg
			
			dlg->RunLD();
			}
			break;
			
		//如果接收到未知事件，则结束程序并报告一个严重错误
		default:
			Panic(EEasyBounceUi);
			break;
		}
	}
// -----------------------------------------------------------------------------
//  CEasyBounceAppUi::HandleStatusPaneSizeChange()
//  程序状态面板大小发生变化时，此函数被框架调用
// -----------------------------------------------------------------------------
//
void CEasyBounceAppUi::HandleStatusPaneSizeChange()
	{
	//空实现
	}

// -----------------------------------------------------------------------------
//  CEasyBounceAppUi::ShowMessage()
//  展示各种Note
// -----------------------------------------------------------------------------
//
void CEasyBounceAppUi::ShowMessage(const TDesC& aMessage, TMessageType aMsgType)
	{
	switch (aMsgType)
		{
		case EInformation:
		    TRAPD(d1,
		          CAknInformationNote* note = new (ELeave) CAknInformationNote;
		          note->ExecuteLD( aMessage );
		        );
		    break;
		case EConfirmation:
		    TRAPD(d2,
		          CAknConfirmationNote* note = new (ELeave) CAknConfirmationNote;
		          note->ExecuteLD( aMessage );
		        );
		    break;
		case EError:
		    TRAPD(d3,
		          CAknErrorNote* note = new (ELeave) CAknErrorNote;
		          note->ExecuteLD( aMessage );
		        );
		    break;
		default:
			break;
		}
	}

// -----------------------------------------------------------------------------
//  CEasyBounceAppUi::ShowQueryDialog()
//  显示查询对话框
// -----------------------------------------------------------------------------
//
TBool CEasyBounceAppUi::ShowQueryDialog(const TDesC& aMessage)
	{
    TInt answer(0);
    TRAPD(err, 
    	HBufC* text = aMessage.AllocLC();
		CAknQueryDialog* dlg =
			new (ELeave) CAknQueryDialog( *text, CAknQueryDialog::ENoTone );
		answer = dlg->ExecuteLD( R_CONFIRMATION_QUERY );
		CleanupStack::PopAndDestroy( text );
    );
    if (err != KErrNone)
        {
        answer = 0;
        }
    return answer != 0;
	}

// -----------------------------------------------------------------------------
//  CEasyBounceAppUi::HandleForegroundEventL()
//  处理前后台切换事件
//  此函数由框架调用
// -----------------------------------------------------------------------------
//
void CEasyBounceAppUi::HandleForegroundEventL(TBool aForeground)
	{
	//获取指向游戏引擎的指针
	TUid gameViewId = TUid::Uid(EMainViewId);
	CEasyBounceGameView* gameView = (CEasyBounceGameView*)this->View(gameViewId);
	CEngine* gameEngine = gameView->Engine();
	
	//如果程序被切换到后台的时候，游戏正在进行，则暂停游戏
	if (EFalse == aForeground)
		{
		if (NULL != gameEngine)
			{
			gameEngine->PauseOrResume();
			}
		}
	
	}

// 文件结束
